package org.trade.goods.service.impl;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.trade.common.constant.TradeEnums;
import org.trade.common.protocol.goods.AddGoodsNumberReq;
import org.trade.common.protocol.goods.AddGoodsNumberRes;
import org.trade.common.protocol.goods.QueryGoodsReq;
import org.trade.common.protocol.goods.QueryGoodsRes;
import org.trade.common.protocol.goods.ReduceGoodsNumberReq;
import org.trade.common.protocol.goods.ReduceGoodsNumberRes;
import org.trade.entity.TradeGoods;
import org.trade.entity.TradeGoodsNumberLog;
import org.trade.entity.TradeGoodsNumberLogKey;
import org.trade.goods.service.IGoodsService;
import org.trade.mapper.TradeGoodsMapper;
import org.trade.mapper.TradeGoodsNumberLogMapper;

@Service
public class GoodsServiceImpl implements IGoodsService{
    @Autowired
	private TradeGoodsMapper tradeGoodsMapper;
    
    @Autowired
    private TradeGoodsNumberLogMapper tradeGoodsNumberLogMapper;
    
	public QueryGoodsRes queryGoods(QueryGoodsReq queryGoodsReq) {
		QueryGoodsRes queryGoodsRes=new QueryGoodsRes();
		queryGoodsRes.setRetCode(TradeEnums.RetEnum.SUCCESS.getCode());
		queryGoodsRes.setRetInfo(TradeEnums.RetEnum.SUCCESS.getDesc());
		try{
			if(queryGoodsReq ==null || queryGoodsReq.getGoodsId()==null){
				throw new Exception("查询商品信息ID不正确");
			}
			TradeGoods tradeGoods=this.tradeGoodsMapper.selectByPrimaryKey(queryGoodsReq.getGoodsId());
			if(tradeGoods !=null){
				BeanUtils.copyProperties(tradeGoods, queryGoodsRes);
			}else{
				throw new Exception("未查询到商品");
			}
		}catch (Exception ex) {
			queryGoodsRes.setRetCode(TradeEnums.RetEnum.FAIL.getCode());
			queryGoodsRes.setRetInfo(ex.getMessage());
		}
		return queryGoodsRes;
	}
    
	@Transactional
	public ReduceGoodsNumberRes reduceGoodsNumber(ReduceGoodsNumberReq reduceGoodsNumberReq) {
		ReduceGoodsNumberRes reduceGoodsNumberRes=new ReduceGoodsNumberRes();
		reduceGoodsNumberRes.setRetCode(TradeEnums.RetEnum.SUCCESS.getCode());
		reduceGoodsNumberRes.setRetInfo(TradeEnums.RetEnum.SUCCESS.getDesc());
		if(reduceGoodsNumberReq ==null || reduceGoodsNumberReq.getGoodsId()==null
        || reduceGoodsNumberReq.getGoodsNumber()==null || reduceGoodsNumberReq.getGoodsNumber()<=0){
			
			throw new RuntimeException("扣减库存请求参数不正确");
		}
		TradeGoods tradeGoods=new TradeGoods();
		tradeGoods.setGoodsId(reduceGoodsNumberReq.getGoodsId());
		tradeGoods.setGoodsNumber(reduceGoodsNumberReq.getGoodsNumber());
		int record=this.tradeGoodsMapper.reduceGoodsNumber(tradeGoods);
		if(record<=0){
			throw new RuntimeException("扣减库存失败");
		}
		TradeGoodsNumberLog tradeGoodsNumberLog=new TradeGoodsNumberLog();
		tradeGoodsNumberLog.setGoodsId(reduceGoodsNumberReq.getGoodsId());
		tradeGoodsNumberLog.setGoodsNumber(reduceGoodsNumberReq.getGoodsNumber());
		tradeGoodsNumberLog.setOrderId(reduceGoodsNumberReq.getOrderId());
		tradeGoodsNumberLog.setLogTime(new Date());
		this.tradeGoodsNumberLogMapper.insert(tradeGoodsNumberLog);
		return reduceGoodsNumberRes;
	}

	public AddGoodsNumberRes addGoodsNumber(AddGoodsNumberReq addGoodsNumberReq) {
		AddGoodsNumberRes addGoodsNumberRes=new AddGoodsNumberRes();
		addGoodsNumberRes.setRetCode(TradeEnums.RetEnum.SUCCESS.getCode());
		addGoodsNumberRes.setRetInfo(TradeEnums.RetEnum.SUCCESS.getDesc());
		if(addGoodsNumberReq ==null || addGoodsNumberReq.getGoodsId()==null
		        || addGoodsNumberReq.getGoodsNumber()==null || addGoodsNumberReq.getGoodsNumber()<=0){
					
					throw new RuntimeException("增加库存请求参数不正确");
				}
		        if(addGoodsNumberReq.getOrderId()!=null){
		        	TradeGoodsNumberLogKey key=new TradeGoodsNumberLogKey();
		        	key.setGoodsId(addGoodsNumberReq.getGoodsId());
		        	key.setOrderId(addGoodsNumberReq.getOrderId());
		        	TradeGoodsNumberLog  tradeGoodsNumberLog=this.tradeGoodsNumberLogMapper.selectByPrimaryKey(key);
		        	if(tradeGoodsNumberLog==null){
		        		throw new RuntimeException("未找到扣库存记录"); 
		        	}
		        }
				TradeGoods tradeGoods=new TradeGoods();
				tradeGoods.setGoodsId(addGoodsNumberReq.getGoodsId());
				tradeGoods.setGoodsNumber(addGoodsNumberReq.getGoodsNumber());
				int record=this.tradeGoodsMapper.reduceGoodsNumber(tradeGoods);
				if(record<=0){
					throw new RuntimeException("扣减库存失败");
				}
				return addGoodsNumberRes;
	}

}
